package sdfi.lee.DAO;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import org.hibernate.Session;

import sdfi.lee.Entity.Change;

public class ChangeDAO {
	public String addChange(Change change) throws Exception 
	{
		Session session=null;
		Connection con=null;
		try
		{
			session=DAO.openSession();
			con=session.connection();
			con.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
			//String sqlget="select platused from platform where id='"+change.getId()+"'";
			//ResultSet rs=con.createStatement().executeQuery(sqlget);
			//int useed=rs.getInt("platused");
			String sqlup="update platform set platused=platused+"+change.getCount().toString()+" where id='"+change.getPid()+"'";
			String sql="insert into mychange(platid,userid,countnb)values('";
			sql+=change.getPid()+"','"+change.getUid()+"','"+change.getCount()+"')";
			System.out.println(sqlup);
			Statement st=con.createStatement();
			st.execute(sql);
			st.execute(sqlup);
			con.commit();
			return "success";
		}
		catch(Exception ee)
		{
			con.rollback();
			System.out.println("Exception when you add change");
			ee.printStackTrace();
			return "error";
		}
		finally
		{	
			con.close();
			DAO.closeSession();
		}
		
	}
}
